export default function(url, onprogress) {
	if (!onprogress) {
		onprogress = () => {}
	}
	return new Promise((resolve, reject) => {

		var xhr = createXHR()
		xhr.open("GET", url, true);

		xhr.responseType = "blob";

		xhr.onload = function() {
			if (this.readyState != 4) {
				return;
			}
			var reader = new FileReader();
			reader.onload = function() {
				resolve(this.result)
			};
			reader.onerror = reject
			reader.readAsDataURL(this.response);
		}
		
		xhr.addEventListener('progress', onprogress);
	
		try{
			xhr.send();
		}catch(e){
			reject(e)
		}
	})
}

function getBase64Image(img) {
	let canvas = document.createElement("canvas");
	canvas.width = img.width;
	canvas.height = img.height;
	let ctx = canvas.getContext("2d");
	ctx.drawImage(img, 0, 0, img.width, img.height);
	let ext = img.src.substring(img.src.lastIndexOf(".") + 1).toLowerCase();
	let dataUrl = canvas.toDataURL("images/png");
	return dataUrl;
};
function createXHR () {
    var XHR = [  //兼容不同浏览器和版本得创建函数数组
        function () { return new XMLHttpRequest () },
        function () { return new ActiveXObject ("Msxml2.XMLHTTP") },
        function () { return new ActiveXObject ("Msxml3.XMLHTTP") },
        function () { return new ActiveXObject ("Microsoft.XMLHTTP") }
    ];
    var xhr = null;
    //尝试调用函数，如果成功则返回XMLHttpRequest对象，否则继续尝试
    for (var i = 0; i < XHR.length; i ++) {
        try {
            xhr = XHR[i]();
        } catch(e) {
            continue  //如果发生异常，则继续下一个函数调用
        }
        break;  //如果成功，则中止循环
    }
    return xhr;  //返回对象实例
}